package models

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/pkg/errors"
	"time"
)

type Inspects struct {
	Id          int64
	Department  *Department `orm:"rel(fk);null"`
	Number      string
	ProductName string
	Pinyin      string
	UnitPrice   float64
	Unit        string
	IsStop      int
	CreatedBy   string
	CreatedAt   int64
	UpdatedBy   string
	UpdatedAt   int64
}

//查询
type InspectsQueryParam struct {
	BaseQueryParam
	Filter string
	IsStop string
}

func (a *Inspects) TableName() string {
	return InspectsTableName()
}

func InspectsTableName() string {
	return TableName("sa_inspects")
}

//查询单条记录
func InspectsFindOne(id int64) *Inspects {
	data := new(Inspects)
	orm.NewOrm().QueryTable(InspectsTableName()).Filter("id", id).One(data)
	return data
}

//查询分页数据
func InspectsPageList(params *InspectsQueryParam) ([]*Inspects, int64) {
	query := orm.NewOrm().QueryTable(InspectsTableName())
	data := make([]*Inspects, 0)
	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}
	cond := orm.NewCondition()
	if len(params.Filter) > 0 {
		cond1 := orm.NewCondition()
		cond = cond.AndCond(cond1.Or("ProductName__contains", params.Filter).Or("Pinyin__contains", params.Filter).Or("number", params.Filter))
	}
	if len(params.IsStop) >0 {
		cond = cond.And("is_stop", params.IsStop)
	}
	query = query.SetCond(cond)
	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}

//删除
func InspectsDeleteById(id int64) error {
	o := orm.NewOrm()
	inspect := new(Inspects)
	orm.NewOrm().QueryTable(InspectsTableName()).Filter("id", id).One(inspect)
	if inspect.Id == 0 {
		return errors.New("项目不存在")
	}
	var is_stop int
	if inspect.IsStop == 1 {
		is_stop = 0
	} else {
		is_stop = 1
	}
	if _, err := o.QueryTable(InspectsTableName()).Filter("id", id).Update(orm.Params{"is_stop": is_stop}); err != nil {
		return err
	}
	return nil
}

func createInspectsNumber() string {
	count, _ := orm.NewOrm().QueryTable(InspectsTableName()).Count()
	return fmt.Sprintf("%s%06d", time.Now().Format("0601021504"), count)
}

func InspectsAdd(c *Inspects) (int64, error) {
	o := orm.NewOrm()
	c.IsStop = 0
	c.CreatedAt = time.Now().Unix()
	c.UpdatedAt = time.Now().Unix()
	c.Number = createInspectsNumber()

	count, _ := o.QueryTable(InspectsTableName()).Filter("ProductName", c.ProductName).Count()
	if count > 0 {
		return 0, errors.New("已存“" + c.ProductName + "”项目")
	}
	id, err := o.Insert(c)
	if err != nil {
		return 0, err
	}
	//增加日志
	InspectsPriceLogAdd(&InspectsPriceLog{Inspects: c, OldPrice: c.UnitPrice, NowPrice: c.UnitPrice, CreatedBy: c.CreatedBy})
	return id, nil
}

//编辑
func InspectsEdit(c *Inspects) (int64, error) {
	o := orm.NewOrm()
	c.UpdatedAt = time.Now().Unix()
	id, err := o.Update(c, "ProductName", "UpdatedAt", "UpdatedBy", "Pinyin", "Unit")
	if err != nil {
		return 0, err
	}
	return id, nil
}

//执行调价
func InspectsEditPrice(inspects_id int64, price float64, created_by string) (error) {
	inspect := InspectsFindOne(inspects_id)
	if inspect.Id == 0 {
		return errors.New("找不到数据")
	}
	to, _ := orm.NewOrm().Begin()
	//增加日志
	if err := InspectsPriceLogAdd(&InspectsPriceLog{Inspects: inspect, OldPrice: inspect.UnitPrice, NowPrice: price, CreatedBy: created_by}); err != nil {
		to.Rollback()
		return err
	}
	inspect.UnitPrice = price
	inspect.UpdatedBy = created_by
	inspect.UpdatedAt = time.Now().Unix()
	if _, err := to.Update(inspect, "UnitPrice", "UpdatedBy", "UpdatedAt"); err != nil {
		to.Rollback()
		return err
	}
	to.Commit()
	return nil
}
